package com.yiwei.zxp.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.yiwei.zxp.R;
import com.yiwei.zxp.api.model.CateListModel;
import com.yiwei.zxp.api.model.ShopIndexModel;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class HomeTypeLeftAdapter extends ItemAdapter<ShopIndexModel.DatasBean.CatsBean, HomeTypeLeftAdapter.DefaultHolder> {


    private final int x32;
    private final int x28;
    private int selectPoi = -1;

    public HomeTypeLeftAdapter(Context context) {
        super(context);
        x32 = context.getResources().getDimensionPixelSize(R.dimen.x32);
        x28 = context.getResources().getDimensionPixelSize(R.dimen.x28);
    }

    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_packet_left, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        ShopIndexModel.DatasBean.CatsBean item = getItem(position);
        holder.item = item;
        holder.poi = position;
        if (selectPoi == position) {
            holder.layoutTotal.setBackgroundResource(R.color.white);
            holder.txtType.setTextColor(0xff000000);
            holder.txtType.setTextSize(TypedValue.COMPLEX_UNIT_PX, x32);
            holder.txtType.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
            holder.viewSelect.setVisibility(View.VISIBLE);
        } else {
            holder.layoutTotal.setBackgroundResource(R.color.bottom_line);
            holder.txtType.setTextColor(0xff757575);
            holder.txtType.setTextSize(TypedValue.COMPLEX_UNIT_PX, x28);
            holder.txtType.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            holder.viewSelect.setVisibility(View.GONE);
        }
        holder.txtType.setText(item.catname);
    }

    class DefaultHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        @BindView(R.id.txt_type)
        TextView txtType;
        @BindView(R.id.view_select)
        View viewSelect;
        @BindView(R.id.layout_total)
        FrameLayout layoutTotal;
        ShopIndexModel.DatasBean.CatsBean item;
        int poi;

        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            layoutTotal.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            selectPoi = poi;
            notifyDataSetChanged();
            if (onChooseListener != null) {
                onChooseListener.onClick(item.id);
            }
        }
    }

    private OnChooseListener onChooseListener;

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void onClick(String cate_id);
    }
}
